package com.jic.purchase.controller;

import com.alibaba.fastjson.JSON;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.purchase.model.entity.PurchaseOrderProduct;
import com.jic.purchase.model.vo.request.PurchaseOrderProductRequest;
import com.jic.purchase.service.PurchaseOrderProductOperationService;
import com.jic.purchase.service.PurchaseOrderProductService;
import com.jic.purchase.utils.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author: MJ
 * @date: 2020年9月19日 17:36:31
 * @remark: 采购单和合同商品关联
 */
@RestController
@RequestMapping("/purchaseOrderProduct")
@Api(tags = {"采购单和合同商品关联"})
@Slf4j
public class PurchaseOrderProductController {

    private final static Logger logger = LoggerFactory.getLogger(PurchaseOrderProductController.class);

    @Resource
    private PurchaseOrderProductService purchaseOrderProductService;



    /**
     * 分页查询采购单商品
     * @param form
     * @return
     */
    @PostMapping("/queryList")
    @ResponseBody
    @ApiOperation(value = "分页查询采购单商品")
    public RestResult<PageResult<PurchaseOrderProduct>> queryList(@RequestBody PurchaseOrderProductRequest form) {
        try {
            log.info("分页查询采购单商品，入参{}", JSON.toJSONString(form));
            Page page = new Page();
            page.setLimit(form.getPageSize());
            page.setStart(form.getPageNum());
            PurchaseOrderProduct purchaseOrderProduct = BeanUtil.copy(form, PurchaseOrderProduct.class);
            PageResult<PurchaseOrderProduct> result = purchaseOrderProductService.queryPage(purchaseOrderProduct, page);
            return RestResult.success(result);
        }catch (Exception e){
            log.error("Purchase queryList error：",e);
            return RestResult.error("9999","系统异常，列表查询失败");
        }
    }


    /**
     * 保存采购单商品
     * @param request
     * @return
     */
    @PostMapping("/savePurchaseOrderProduct")
    @ResponseBody
    @ApiOperation(value = "保存采购单商品")
    public RestResult<Long> savePurchaseOrderProduct(@RequestBody PurchaseOrderProductRequest request) {
        try {
            log.debug("savePurchaseOrderProduct " + request);
            long id = purchaseOrderProductService.foreachInsert(request);
            return RestResult.success(id);
        }catch (Exception e){
            log.error("Purchase savePurchaseOrderProduct error：",e);
            return RestResult.error("9999","系统异常，保存采购单商品失败");
        }
    }

    /**
     * 收货发货数量变更
     * @param request
     * @return
     */
    @PostMapping("/editProductNum")
    @ResponseBody
    @ApiOperation(value = "收货发货数量变更")
    public RestResult<Long> editProductNum(@RequestBody PurchaseOrderProductRequest request) {
        try {
            log.debug("editProductNum " + request);
            long id = purchaseOrderProductService.editProductNum(request);
            return RestResult.success(id);
        }catch (Exception e){
            log.error("Purchase editProductNum error：",e);
            return RestResult.error("9999","系统异常，操作失败");
        }
    }



}
